package com.hzy.servlet;

import com.hzy.pojo.Booking;
import com.hzy.service.BookingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

/**
 * @Package: com.hzy.servlet
 * @ClassName: AddServlet
 * @Author: 862982795
 * @CreateTime: 2021/07/01 21:20
 * @Description:
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");

        // 创建Booking对象存储请求的值
        Booking booking = new Booking();

        booking.setUserName(req.getParameter("name"));
        booking.setCardId(req.getParameter("cardId"));
        booking.setBookingDate(Date.valueOf(req.getParameter("date")));
        booking.setStatus(0);

        BookingService service = new BookingService();
        int i = service.addBooking(booking);

        PrintWriter out = resp.getWriter();
        if (i > 0) {
            out.print("<script>alert('添加成功!');location.href='ListServlet'</script>");
        } else {
            out.print("<script>alert('添加失败!');history.back();</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(req, resp);
    }
}
